<?php

namespace app\http\Controllers\Manage;

use think\App;
use think\Request;
use app\repository\common\Module;
use app\common\lib\UploadFile;

class ModuleController extends BaseController
{
    private $rep;

    public function __construct(App $app, Request $request)
    {
        parent::__construct($app, $request);
        $this->rep = new Module();
    }


    /**
     * 功能模块页面
     * @return string
     */
    public function index()
    {
        $data = [];
        return $this->view('module/list', $data);
    }


    /**
     * 功能模块列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRecordList(Request $request)
    {
        $param = $request->param();
        $page = isset($param['page']) ? intval($param['page']) : 1;
        $limit = isset($param['limit']) ? intval($param['limit']) : 1;
        $count = $this->rep->getModuleRecordCount($param);
        $parger = turnPageToStartEnd($page, $limit);
        $list = $this->rep->getModuleRecordList($param, $parger['start'], $parger['limit']);
        return apiResponse(STATUS_CODE_SUCCESS, 'success', formatPager($count, $list, $page, $limit));
    }


    /**
     * 功能编辑页面
     * @param $id
     * @return string
     */
    public function moduleInfoPage($id, Request $request)
    {
        $data = [
            'id' => $id,
            'info' => [],
        ];
        if ($id > 0) {
            $info = $this->rep->getModuleInfoById($id);
            if (!$info) return $this->showErrorPage(STATUS_CODE_NOTFOUND, '数据不存在');
            $data['info'] = $info;
        }
        return $this->view('module/edit', $data);
    }

    /**
     * 添加功能模块
     * @return void
     */
    public function createModule(Request $request)
    {
        $param = $request->param();
        $res = $this->rep->createModule($param);
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return apiResponse($res['status'], $res['msg']);
        }
        return apiResponse(STATUS_CODE_SUCCESS, '保存成功');
    }

    /**
     * 修改功能模块
     * @param $id
     * @param Request $request
     * @return void
     */
    public function updateModule($id, Request $request)
    {
        $params = $request->param();
        $res = $this->rep->updateModule($id, $params);
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return apiResponse($res['status'], $res['msg']);
        }
        return apiResponse(STATUS_CODE_SUCCESS, '修改成功');
    }

    /**
     * 删除功能模块
     * @return void
     */
    public function deleteModule($id, Request $request)
    {
        $res = $this->rep->deleteModule($id);
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return apiResponse($res['status'], $res['msg']);
        }
        return apiResponse(STATUS_CODE_SUCCESS, 'success');
    }

    /**
     * 权限列表页面
     * @return void
     */
    public function rightListPage(Request $request)
    {
        $params = $request->param();
        $data = [
            'moduleList' => $this->rep->getModuleRecordList([], 0, 0),
            'clientList' => $this->rep->getClientsList([], 0, 0)
        ];
        return $this->view('module/rights_list', $data);
    }

    /**
     * 权限列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRightsList(Request $request)
    {
        $param = $request->param();
        $list = $this->rep->getRightsList($param, 0, 0);
        return apiResponse(STATUS_CODE_SUCCESS, 'success', $list);
    }


    /**
     * 权限详情页面
     * @param $id
     * @param Request $request
     * @return string
     */
    public function rightEditPage($id, Request $request)
    {
        $params = $request->param();
        $data = [
            'id' => $id,
            'info' => [
                'id' => 0,
                'parent_id' => 0,
                'module_id' => 0,
                'is_menu' => 0,
                'is_show' => 0,
                'resource' => '',
                'clients_id' => 0,
                'resource_list' => []
            ],
            'moduleList' => $this->rep->getModuleRecordList([], 0, 0),
            'parent_id' => $params['parent_id'] ?? 0,
            'clientsList' => $this->rep->getClientsList(),
        ];
        if ($id > 0) {
            $data['info'] = $this->rep->getRightsInfoById($id);
            if ($data['info']) {
                $data['parent_id'] = $data['info']['parent_id'];
            }
        }
        return $this->view('module/rights_info', $data);
    }


    /**
     * 上传菜单图标
     * @return \Illuminate\Http\JsonResponse
     */
    public function uploadMenuIcon()
    {
        return $this->uploadFile('menu', ['gif', 'png', 'jpg', 'jpeg'], 10240);
    }

    /**
     * 添加权限
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function createRights(Request $request)
    {

        $res = $this->rep->createRights($request->param());
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return apiResponse($res['status'], $res['msg']);
        }
        return apiResponse(STATUS_CODE_SUCCESS, '保存成功');
    }

    /**
     * 修改权限
     * @param $id
     * @param Request $request
     * @return void
     */
    public function updateRights($id, Request $request)
    {
        $param = $request->param();
        $res = $this->rep->updateRights($id, $param);
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return apiResponse($res['status'], $res['msg']);
        }
        return apiResponse(STATUS_CODE_SUCCESS, '修改成功');
    }

    /**
     * 删除权限
     * @param $id
     * @return JsonResponse
     */
    public function deleteRights($id)
    {
        $res = $this->rep->deleteRights($id);
        if ($res['status'] != STATUS_CODE_SUCCESS) {
            return apiResponse($res['status'], $res['msg']);
        }
        return apiResponse(STATUS_CODE_SUCCESS, '删除成功');
    }

    /**
     * 切换修改权限是否菜单
     */
    public function switchRightsIsMenu(Request $request)
    {
        $param = $request->param();
        $id = $param['id'] ?? 0;
        $value = $param['value'] ?? 0;
        $res = $this->rep->switchRightsIsMenu($id, $value);
        return apiResponse($res['status'], $res['msg']);
    }

    /**
     * 切换修改权限是否显示
     * @param Request $request
     * @return JsonResponse
     */
    public function switchRightsIsShow(Request $request)
    {
        $param = $request->param();
        $id = $param['id'] ?? 0;
        $value = $param['value'] ?? 0;
        $res = $this->rep->switchRightsIsShow($id, $value);
        return apiResponse($res['status'], $res['msg']);
    }


}